﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpHomeEngine;

namespace Insteon.Devices
{
    public class TriggerLinc : InsteonDevice
    {
        TagBoolean Opened;

        public TriggerLinc(InsteonInterface Interface, string Name, InsteonAddress Address)
            : base(Interface, Name, Address)
        {
            Opened = new TagBoolean(Interface, this, "Opened");
        }

        internal override Commands.Command GenerateStateChangeCommand(Tag Tag, object Value)
        {
            //I dont do tag state change requests
            return null;
        }


        internal override void HandleUnsolicitedMessage(Messages.Message Message)
        {
//Closing
//#02 #50 #14 #b4 #ed #00 #00 #01 #cb #13 #01 
//143445.860  0002 StandardMessage      (to:00.00.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1:13 c2: 1)     : 02 50 14 b4 ed 00 00 01 cb 13 01  Unsolicited
//#02 #50 #14 #b4 #ed #00 #00 #01 #cb #13 #01 
//143446.043  0003 StandardMessage      (to:00.00.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1:13 c2: 1)     : 02 50 14 b4 ed 00 00 01 cb 13 01  Unsolicited
//#02 #50 #14 #b4 #ed #aa #aa #aa #41 #13 #01 
//143446.237  0004 StandardMessage      (to:AA.AA.AA) (fr:14.B4.ED) (Flags: ALLLinkCleanup   False 0 1) (c1:13 c2: 1)     : 02 50 14 b4 ed aa aa aa 41 13 01  Unsolicited
//#02 #50 #14 #b4 #ed #13 #01 #01 #cb #06 #00 
//143446.445  0005 StandardMessage      (to:13.01.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1: 6 c2: 0)     : 02 50 14 b4 ed 13 01 01 cb 06 00  Unsolicited
//#02 #50 #14 #b4 #ed #13 #01 #01 #cb #06 #00 
//143446.637  0006 StandardMessage      (to:13.01.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1: 6 c2: 0)     : 02 50 14 b4 ed 13 01 01 cb 06 00  Unsolicited

//opening            
//#02 #50 #14 #b4 #ed #00 #00 #01 #cb #11 #01 
//143542.840  0002 StandardMessage      (to:00.00.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1:11 c2: 1)     : 02 50 14 b4 ed 00 00 01 cb 11 01  Unsolicited
//#02 #50 #14 #b4 #ed #00 #00 #01 #cb #11 #01 
//143543.033  0003 StandardMessage      (to:00.00.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1:11 c2: 1)     : 02 50 14 b4 ed 00 00 01 cb 11 01  Unsolicited
//#02 #50 #14 #b4 #ed #aa #aa #aa #41 #11 #01 
//143543.222  0004 StandardMessage      (to:AA.AA.AA) (fr:14.B4.ED) (Flags: ALLLinkCleanup   False 0 1) (c1:11 c2: 1)     : 02 50 14 b4 ed aa aa aa 41 11 01  Unsolicited
//#02 #50 #14 #b4 #ed #11 #01 #01 #cb #06 #00 
//143543.432  0005 StandardMessage      (to:11.01.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1: 6 c2: 0)     : 02 50 14 b4 ed 11 01 01 cb 06 00  Unsolicited
//#02 #50 #14 #b4 #ed #11 #01 #01 #cb #06 #00 
//143543.622  0006 StandardMessage      (to:11.01.01) (fr:14.B4.ED) (Flags: AllLinkBroadcast False 2 3) (c1: 6 c2: 0)     : 02 50 14 b4 ed 11 01 01 cb 06 00  Unsolicited

            if (Message is Messages.Insteon.StandardMessage)
            {
                Messages.Insteon.StandardMessage Mess = (Messages.Insteon.StandardMessage)Message;
                if (Mess.From == this.InsteonAddress)
                {
                    if (Mess.Command1 == 0x13)
                    {
                        //Door Closing
                        //this.InsteonInterface.
                        UnsolicitedTagChange(Opened, false);
                    }
                    if (Mess.Command1 == 0x11)
                    {
                        //Door opening
                        UnsolicitedTagChange(Opened, true);
                    }
                }
            }
        
        }

        internal override Commands.Command GenerateStateRequest(Tag Tag)
        {
            //triggerlinc is rf only and they go into a power saving mode where they cant be queried.
            //So this wont do anything.
            //and get's will only timeout becuase the device wont reply.
            if (Tag == Opened)
            {
                Commands.Insteon.StandardCommand Command = new Commands.Insteon.StandardCommand(this.InsteonAddress, 0x19, 0x00);
                return Command;
            }
            return null;
        }


        internal override object ReadStateRequestResponse(Commands.Command Command, Tag Tag)
        {
            if (Command.DeviceResponse == null)
            {
                return null;
            }
            if (!(Command.DeviceResponse is Messages.Insteon.StandardMessage))
            {
                return null;
            }
            Messages.Insteon.StandardMessage Message = (Messages.Insteon.StandardMessage)Command.DeviceResponse;
            if (Tag == Opened)
            {
                Command.Value = Opened.CastValue(Message.Command2);
            }
            return null;
        }
    }

}
